package com.dingding.web.activity;

import javax.annotation.Resource;

import com.dingding.common.core.util.ToolUtil;
import com.dingding.common.core.util.tools.wechat.WechatCode;
import com.dingding.facde.share.IShareActivityFacade;
import com.dingding.facde.share.dto.CheckIsShareResult;
import com.dingding.facde.share.dto.RegActivityResult;
import com.dingding.facde.share.dto.StartShareResult;
import com.dingding.web.base.action.BaseAction;

/**
 * @Description:中秋节活动的action
 * @author fengshuonan
 * @date 2016年9月5日 下午2:17:05
 * @version 叮叮搭车
 * @since 2.4
 */
@Deprecated
@SuppressWarnings("serial")
public class MoonActivityAction extends BaseAction{
	
	@Resource(name = "shareActivityFacade")
	private IShareActivityFacade shareActivityFacade;
	
	private static final String activityId = "7";
	
	/**
	 * @Description:检查是否已经发起国分享链接
	 * @author fengshuonan
	 * @return String
	 * @since 2.4
	 */
	public String checkIsShare(){
		
		String userId = super.getPara("userId");
		
		if(ToolUtil.isBlank(userId)){
			super.writeResponseJsonp(400,"所传数据为空");
			return NONE;
		}
		
		CheckIsShareResult checkIsShare = this.shareActivityFacade.checkIsShare(userId, activityId);
		
		if(checkIsShare != null){
			super.writeResponseJsonp(checkIsShare);
		}else{
			super.writeResponseJsonp(500,"服务器异常");
		}
		
		return NONE;
	}
	
	/**
	 * @Description:发起分享链接
	 * @author fengshuonan
	 * @since 2.4
	 */
	public String startShare(){
		
		String userId = super.getPara("userId");
		String giftId = super.getPara("giftId");
		String[] gifts = {giftId};
		
		if(ToolUtil.isBlank(userId) || ToolUtil.isBlank(giftId)){
			super.writeResponseJsonp(400,"所传数据为空");
			return NONE;
		}
		
		StartShareResult startShare = this.shareActivityFacade.startShare(activityId, userId, gifts);
		
		if(startShare == null){
			super.writeResponseJsonp(500,"服务器异常");
		}else{
			super.writeResponseJsonp(startShare);
		}
		
		return NONE;
	}
	
	
	/**
	 * @Description:中秋活动点击注册
	 * @author fengshuonan
	 * @since 2.4
	 */
	public String regMoonActivity(){
		
		String telephone = super.getPara("telephone");
		String messageCode = super.getPara("messageCode");
		String shareKey = super.getPara("shareKey");
		
		if(ToolUtil.isBlank(telephone) || ToolUtil.isBlank(messageCode)){
			super.writeResponseJsonp(400,"所传数据为空");
			return NONE;
		}
		
		RegActivityResult regUser4Activity = this.shareActivityFacade.regUser4Activity(activityId, telephone, messageCode, shareKey, "0");
		
		if(regUser4Activity == null){
			super.writeResponseJsonp(500,"服务器异常");
		}else{
			if(regUser4Activity.isMessageCodeTrue() == false){
				super.writeResponseJsonp(400,"验证码错误");
				return NONE;
			}
			super.writeResponseJsonp(regUser4Activity);
		}
		
		return NONE;
	}
	
	/**
	 * @Description:获取电话手机号验证码
	 * @author fengshuonan
	 * @return String
	 * @since 2.4
	 */
	public String getTelePhoneCode(){
		
		String telephone = super.getPara("telephone");
		
		if(ToolUtil.isBlank(telephone)){
			super.writeResponseJsonp(400,"所传数据为空");
			return NONE;
		}
		
		boolean flag = this.shareActivityFacade.sendPhoneMsg(telephone);
		
		if(flag){
			super.writeResponseJsonp(200,"发送成功");
		}else{
			super.writeResponseJsonp(500,"服务器异常");
		}
		return NONE;
	}
	
	/**
	 * @Description:微信验证
	 * @author fengshuonan
	 * @since 2.4
	 */
	public String weChatValidate(){
		
		String telephone = super.getPara("telephone");
		String messageCode = super.getPara("messageCode");
		
		if(ToolUtil.isBlank(telephone) || ToolUtil.isBlank(messageCode)){
			super.writeResponseJsonp(400,"所传数据为空");
			return NONE;
		}
		
		RegActivityResult regUser4Activity = this.shareActivityFacade.regUser4Activity(
				activityId, telephone, messageCode, null, "0");
		
		if(regUser4Activity == null){
			super.writeResponseJsonp(500,"服务器异常");
			return NONE;
		
		}else{
			if(regUser4Activity.isMessageCodeTrue() == false){
				super.writeResponseJsonp(400,"验证码错误");
				return NONE;
			}else{
				//生成js-sdk所需要的变量
				super.validation = super.getWechatValidation(super.request);
				super.validation.setAppId(WechatCode.appID);
				super.request.setAttribute("validation", validation);
				
				if(regUser4Activity.getGifts() != null){
					String gift = regUser4Activity.getGifts()[0];
					super.request.setAttribute("gift", gift);
				}
				
				int inviteNum = regUser4Activity.getInviteNum();
				int status = regUser4Activity.getStatus();
				String userId = regUser4Activity.getUserId();
				
				super.request.setAttribute("inviteNum", inviteNum);
				super.request.setAttribute("status", status);
				super.request.setAttribute("userId", userId);

				return "moonShare";
			}
		}
	}
}
